import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

/**
 * Capitol Reef Team, Summer 2012 [ICS 314]
 *
 * This class file consists of a simple program that reads in user inputs and
 * creates a .ics (calendar file) and follows the standards outlined in the RFC 5445.
 * 
 * Support has been added for the following parameters of a ICS file, please
 * refer to RFC 5445 for specific requirements.
 * 
 * Version (Section 3.7.4 in RFC 5445)
 * Property Name: VERSION
 *
 * Purpose: This property specifies the identifier corresponding to the highest version number or the minimum and maximum range of the iCalendar specification that is required in order to interpret the iCalendar object.
 *
 * Value Type: TEXT
 *
 * Property Parameters: IANA and non-standard property parameters can be specified on this property.
 *
 * Conformance: This property MUST be specified once in an iCalendar object.
 *
 * Description: A value of "2.0" corresponds to this memo.
 *
 * Format Definition: This property is defined by the following notation:
 *
 * version    = "VERSION" verparam ":" vervalue CRLF
 *
 * verparam   = *(";" other-param)
 *
 * vervalue   = "2.0"         ;This memo
 *                            / maxver
 *                            / (minver ";" maxver)
 *
 *                            minver     = <A IANA-registered iCalendar version identifier>
 *                            ;Minimum iCalendar version needed to parse the iCalendar object.
 *
 *                            maxver     = <A IANA-registered iCalendar version identifier>
 *                            ;Maximum iCalendar version needed to parse the iCalendar object.
 * 
 * Example:  The following is an example of this property: VERSION:2.0
 * 
 * 
 * 
 *  
 * 
 * 
 * Classification (3.8.1.3)
 * Comment (3.8.14)
 * Priority (3.8.1.9)
 * Location (3.8.1.7)
 * Resources (3.8.1.10)
 * DTSTART (3.8.2.4)
 * DTEND (3.8.2.2)
 * Time zone identifer (3.8.3.1)
 * 
 * @author James Knight
 * @author Allan Wu
 * @author Jerry Koko
 *
 */
public class CapitolReefGoogleCalendarNoGUI {
	
	// This is necessary to format the timestamp to iCal specs (Allan)
	private static SimpleDateFormat iCalendarDateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmm'00'");
	 

	public static void main(String args[]) throws IOException
	{
	
		// This is the Command-line version of Calendar
		Scanner input = new Scanner(System.in);
		
		// Email of user
		System.out.println("What is your email? ");
		System.out.println();
		String email = input.nextLine();
		
		// Event 
		System.out.println("Name of Event: ");
		System.out.println();
		String eventName = input.nextLine();
		
		// Event's date
		System.out.println("Please enter a Event date: ");
		System.out.println();
		String eventDate = input.nextLine();
		
		// Event's start time
		System.out.println("Please enter a start time: ");
		System.out.println();
		String startTime = input.nextLine();
		
		// Event's end time
		System.out.println("Please enter an end time: ");
		System.out.println();
		String endTime = input.nextLine();
		
		// Event's priority
		System.out.println("Please enter a priority (0,25,50,100): ");
		System.out.println();
		int priority = input.nextInt();
		
		// .ics file name
		System.out.println("Name of file: ");
		System.out.println();
		String name = input.nextLine();
		name = name + ".ics";  // appends .ics suffix
		
		// Time Stamp
		Calendar cal = new GregorianCalendar();
        Date timeStamp = cal.getTime();


//check the icalendar spec in order to build a more complicated meeting request (will add more--Allan)
String calendarContent =
        "BEGIN:VCALENDAR\n" +
                "METHOD:REQUEST\n" +
                "PRODID:-//Google Inc//Google Calendar 70.9054//EN\n" +
                "VERSION:2.0\n" +
                "CALSCALE:GREGORIAN\n" +
                "METHOD:PUBLISH\n" +
                "X-WR-CALNAME:" + email + "\n" +
                "X-WR-TIMEZONE:" + cal.getTimeZone().getID() + "\n" +
                "BEGIN:VEVENT\n" +
                "DTSTAMP:" + iCalendarDateFormat.format(timeStamp) + "Z\n" +
                "DTSTART:" + iCalendarDateFormat.format(timeStamp)+ "Z\n" +
                "DTEND:"  + iCalendarDateFormat.format(timeStamp)+ "Z\n" +
                "SUMMARY:" + eventName + "\n" +
                "UID:324\n" +
                "ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE:MAILTO:organizer@yahoo.com\n" +
                "ORGANIZER:MAILTO:organizer@yahoo.com\n" +
                "LOCATION:on the net\n" +
                "DESCRIPTION:learn some stuff\n" +
                "SEQUENCE:0\n" +
                "PRIORITY:" + priority + "\n" +
                "CLASS:PUBLIC\n" +
                "STATUS:CONFIRMED\n" +
                "TRANSP:OPAQUE\n" +
                "BEGIN:VALARM\n" +
                "ACTION:DISPLAY\n" +
                "DESCRIPTION:REMINDER\n" +
                "TRIGGER;RELATED=START:-PT00H15M00S\n" +
                "END:VALARM\n" +
                "END:VEVENT\n" +
                "END:VCALENDAR";

// calendarPart.addHeader("Content-Class", "urn:content-classes:calendarmessage");
// calendarPart.setContent(calendarContent, "text/calendar;method=CANCEL");

	// Finally, iterate the calendarContent to screen (demo) -- Allan  
	System.out.println(calendarContent);


	// File output section  	
  	FileWriter fw = new FileWriter(name);
  	BufferedWriter bw = new BufferedWriter(fw);
  	PrintWriter outFile = new PrintWriter(bw);
  
  	// Produces file 
  	outFile.println(calendarContent);
  
  	// Close file stream
  	outFile.close();
  
  	// Interface response to User
  	System.out.println();
  	System.out.println("Success! An output file has been created: " + name);
  
  	}
	
}